package com.powernode.service.impl;

import com.powernode.commons.Result;
import com.powernode.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 *   人 可以给动物看病(可能猫，可能是鱼)，问：治疗行为    策略模式  行为型
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private DiscoveryClient discoveryClient;



    @Override
    public Result goodsList() {
        //根据服务实例名称拉取指定注册中心的服务地址
        List<ServiceInstance> product = discoveryClient.getInstances("product");
        ServiceInstance serviceInstance = product.get(0);
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();


        Result forObject = restTemplate.getForObject("http://"+host+":"+port+"/goods/items", Result.class);
        return forObject;
    }
}
